import React, { useEffect, useState } from 'react';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';

/** API 请求配置 */
const API_CONFIG = {
  SERVICES_PAGE_SIZE: 200,
  SERVERLESS_SERVICE_CATEGORY: 'serverless-service',
};



/** 筛选器默认值 */
const FILTER_DEFAULTS = {
  ALL: 'all',
};

// ============================================================================
// 工具函数 - 数据处理相关
// ============================================================================

/**
 * 获取服务的唯一厂商列表
 * @param {Array} services - 服务列表
 * @returns {Array} 排序后的厂商列表
 */
function extractUniqueVendors(services) {
  const vendorSet = new Set();

  services.forEach((service) => {
    service.operations?.forEach((operation) => {
      if (operation.vendor_info?.label) {
        vendorSet.add(operation.vendor_info.label);
      }
    });
  });

  return Array.from(vendorSet).sort();
}

/**
 * 获取服务的唯一分类列表（按 sort_order 排序）
 * @param {Array} services - 服务列表
 * @returns {Array} 排序后的分类列表
 */
function extractUniqueCategories(services) {
  const categoryMap = new Map();

  services.forEach((service) => {
    if (!service.tags?.length) return;

    const categoryTag = service.tags.find((tag) =>
      tag.categories?.some(
        ({ slug }) => slug === API_CONFIG.SERVERLESS_SERVICE_CATEGORY,
      ),
    );

    if (categoryTag?.name && categoryTag.name !== '-') {
      categoryMap.set(categoryTag.name, categoryTag);
    }
  });

  // 按 sort_order 排序（倒序）
  return Array.from(categoryMap.values()).sort((a, b) => {
    const orderA = a.sort_order ?? Number.MAX_SAFE_INTEGER;
    const orderB = b.sort_order ?? Number.MAX_SAFE_INTEGER;

    return orderA !== orderB ? orderB - orderA : 0;
  });
}

/**
 * 获取服务的分类名称
 * @param {Array} tags - 标签列表
 * @returns {string} 分类名称，未找到时返回 '-'
 */
function getServiceCategoryName(tags) {
  if (!tags?.length) return '-';

  const categoryTag = tags.find((tag) =>
    tag.categories?.some(
      ({ slug }) => slug === API_CONFIG.SERVERLESS_SERVICE_CATEGORY,
    ),
  );

  return categoryTag?.name || '-';
}

/**
 * 按 name 合并操作列表
 * @param {Array} operations - 操作列表
 * @returns {Array} 合并并排序后的操作列表
 */
function mergeOperations(operations) {
  // 按 name 合并操作
  const operationMap = new Map();
  operations.forEach((operation) => {
    const name = operation.name;
    if (!name) return;
    if (!operationMap.has(name)) {
      operationMap.set(name, operation);
    }
  });
  return Array.from(operationMap.values());
}

// ============================================================================
// UI 组件 - 错误和加载状态
// ============================================================================

/**
 * 错误消息组件
 */
function ErrorMessage({ error }) {
  return (
    <p
      style={{
        border: '1px solid #f44336',
        background: '#f44336',
        color: 'white',
        borderRadius: 4,
        padding: 10,
      }}
    >
      无法获取 Serverless API 服务列表，请向我们发送邮件反馈该问题：
      <a
        href="mailto:gitee-ai@oschina.cn"
        style={{ color: 'white', textDecoration: 'underline' }}
      >
        gitee-ai@oschina.cn
      </a>
      {error && (
        <>
          <br />
          错误信息：{error}
        </>
      )}
    </p>
  );
}

/**
 * 加载状态组件
 */
function LoadingMessage() {
  return (
    <p style={{ border: '1px solid #CCC', borderRadius: 4, padding: 10 }}>
      加载 Serverless API 服务列表...
    </p>
  );
}

// ============================================================================
// UI 组件 - 筛选器
// ============================================================================

/**
 * 筛选器组件
 */
function FilterControls({
  selectedVendor,
  selectedTag,
  vendors,
  tags,
  serviceCount,
  operationCount,
  onVendorChange,
  onTagChange,
}) {
  const selectStyle = {
    padding: '5px 8px',
    borderRadius: 4,
    border: 0,
    outline: '1px solid #ccc',
    borderRight: '8px solid transparent',
  };

  return (
    <div
      style={{
        marginBottom: 10,
        fontSize: 14,
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'space-between',
        flexWrap: 'wrap',
        gap: '8px',
      }}
    >
      <div
        style={{
          display: 'flex',
          alignItems: 'center',
          flexWrap: 'wrap',
          gap: '8px',
          flex: 1,
          minWidth: 0,
        }}
      >
        {/* 厂商筛选器 */}
        {/*<div*/}
        {/*  style={{*/}
        {/*    display: 'flex',*/}
        {/*    alignItems: 'center',*/}
        {/*    minWidth: 0,*/}
        {/*  }}*/}
        {/*>*/}
        {/*  <label*/}
        {/*    htmlFor="vendor-filter"*/}
        {/*    style={{*/}
        {/*      marginRight: 5,*/}
        {/*      fontSize: isMobile ? '12px' : '14px',*/}
        {/*      whiteSpace: 'nowrap',*/}
        {/*    }}*/}
        {/*  >*/}
        {/*    {isMobile ? '厂商：' : '选择厂商：'}*/}
        {/*  </label>*/}
        {/*  <select*/}
        {/*    id="vendor-filter"*/}
        {/*    value={selectedVendor}*/}
        {/*    onChange={(e) => onVendorChange(e.target.value)}*/}
        {/*    style={isMobile ? mobileSelectStyle : selectStyle}*/}
        {/*  >*/}
        {/*    <option value={FILTER_DEFAULTS.ALL}>全部厂商</option>*/}
        {/*    {vendors.map((vendor) => (*/}
        {/*      <option key={vendor} value={vendor}>*/}
        {/*        {vendor}*/}
        {/*      </option>*/}
        {/*    ))}*/}
        {/*  </select>*/}
        {/*</div>*/}

        {/* 分类筛选器 */}
        <div
          style={{
            display: 'flex',
            alignItems: 'center',
            minWidth: 0,
          }}
        >
          <label
            htmlFor="category-filter"
            style={{
              marginRight: 5,
              fontSize: '14px',
              whiteSpace: 'nowrap',
            }}
          >
            分类：
          </label>
          <select
            id="category-filter"
            value={selectedTag}
            onChange={(e) => onTagChange(e.target.value)}
            style={selectStyle}
          >
            <option value={FILTER_DEFAULTS.ALL}>全部分类</option>
            {tags.map((tag) => (
              <option key={tag.name} value={tag.name}>
                {tag.name}
              </option>
            ))}
          </select>
        </div>
      </div>

      {/* 统计信息 */}
      <span
        style={{
          fontSize: '14px',
          whiteSpace: 'nowrap',
          flexShrink: 0,
        }}
      >
        共 {serviceCount} 个模型, {operationCount} 个接口
      </span>
    </div>
  );
}

// ============================================================================
// UI 组件 - 服务表格
// ============================================================================

/**
 * 服务表格组件
 */
function ServicesTable({ services, showOperations, hostUrl }) {
  const isMobile = typeof window !== 'undefined' && window.innerWidth <= 768;

  const renderOperationCell = (operation, ident, createdAt) => (
    <td width={180} style={{ verticalAlign: 'middle', textAlign: 'right' }}>
      {/*<span style={{ fontSize: 10 }}>*/}
      {/*  {operation?.vendor_info?.label || '未知厂商'}*/}
      {/*</span>{' '}*/}
      <a
        title={operation.description}
        href={`${hostUrl}/serverless-api?model=${ident}&operation=${operation.id}`}
        target="blank"
        style={{ wordBreak: 'break-all' }}
      >
        {operation.name}
      </a>
      {/*<span style={{ fontSize: 10, color: '#666' }}>*/}
      {/*  {new Date(operation.created_at || createdAt).toLocaleDateString(*/}
      {/*    'zh-CN',*/}
      {/*  )}*/}
      {/*</span>*/}
    </td>
  );

  /**
   * 渲染服务基本信息单元格
   */
  const renderServiceInfoCells = (service, operationCount) => (
    <>
      <td
        width={200}
        style={{ verticalAlign: 'middle' }}
        rowSpan={operationCount}
      >
        <a
          href={
            service.ident
              ? `${hostUrl}/serverless-api?model=${service.ident}`
              : `${hostUrl}/serverless-api?service=${service.id}`
          }
          target="blank"
        >
          {service.model_info.name}
        </a>
      </td>
      <td
        style={{ verticalAlign: 'middle' }}
        width={140}
        rowSpan={operationCount}
      >
        {getServiceCategoryName(service.tags) || '-'}
      </td>
      <td
        style={{
          verticalAlign: 'middle',
          display: isMobile ? 'none' : 'table-cell',
        }}
        title={service.remark}
        rowSpan={operationCount}
      >
        {service.remark ?? ''}
      </td>
    </>
  );

  return (
    <table className="serverless-api-services">
      <thead>
        <tr>
          <th>模型名称</th>
          <th>分类</th>
          <th style={{ display: isMobile ? 'none' : 'table-cell' }}>简介</th>
          {showOperations && <th style={{ textAlign: 'right' }}>接口</th>}
        </tr>
      </thead>
      <tbody>
        {services.map((service, serviceIndex) => {
          const { filteredOps = [], created_at, ident } = service;
          const hasOperations = filteredOps.length > 0;

          return hasOperations ? (
            // 有操作的服务 - 每个操作一行
            filteredOps.map((operation, opIndex) => (
              <tr key={`${serviceIndex}-${opIndex}`}>
                {opIndex === 0 &&
                  renderServiceInfoCells(service, filteredOps.length)}
                {renderOperationCell(operation, ident, created_at)}
              </tr>
            ))
          ) : (
            // 无操作的服务 - 单行显示
            <tr key={serviceIndex}>
              <td style={{ verticalAlign: 'middle' }}>
                <a
                  href={
                    ident
                      ? `${hostUrl}/serverless-api?model=${ident}`
                      : `${hostUrl}/serverless-api?service=${service.id}`
                  }
                  target="blank"
                >
                  {service.model_info.name}
                </a>
              </td>
              <td style={{ verticalAlign: 'middle' }} width={110}>
                {getServiceCategoryName(service.tags) || '-'}
              </td>
              <td
                style={{
                  verticalAlign: 'middle',
                  display: isMobile ? 'none' : 'table-cell',
                }}
                title={service.remark}
              >
                {service.remark ?? ''}
              </td>
              <td width={360} style={{ textAlign: 'right' }}>
                -
              </td>
            </tr>
          );
        })}
      </tbody>
    </table>
  );
}

// ============================================================================
// 自定义 Hooks - 状态管理
// ============================================================================

/**
 * 筛选参数管理 Hook
 */
function useFilterParams(
  defaultVendor = FILTER_DEFAULTS.ALL,
  defaultTag = FILTER_DEFAULTS.ALL,
) {
  const [selectedVendor, setSelectedVendor] = useState(defaultVendor);
  const [selectedTag, setSelectedTag] = useState(defaultTag);

  // 初始化：使用默认值
  useEffect(() => {
    if (defaultVendor !== FILTER_DEFAULTS.ALL) {
      setSelectedVendor(defaultVendor);
    }

    if (defaultTag !== FILTER_DEFAULTS.ALL) {
      setSelectedTag(defaultTag);
    }
  }, [defaultVendor, defaultTag]);

  return {
    selectedVendor,
    selectedTag,
    setSelectedVendor,
    setSelectedTag,
  };
}

/**
 * 服务数据获取 Hook
 */
function useServicesData(hostUrl) {
  const [loading, setLoading] = useState(true);
  const [services, setServices] = useState([]);
  const [vendors, setVendors] = useState([]);
  const [tags, setTags] = useState([]);
  const [showOperations, setShowOperations] = useState(false);
  const [error, setError] = useState(null);

  useEffect(() => {
    let isMounted = true;

    /**
     * 获取操作数据（优先方案）
     */
    async function fetchOperationsData() {
      try {
        const response = await fetch(`${hostUrl}/api/pay/service/operations`);

        if (response.status !== 200) {
          await fetchServicesData();
          return;
        }

        const data = await response.json();
        const processedServices = data
          .map(({ operations, service }) => {
            // 计算厂商最早日期
            const vendorEarliestDates = operations.reduce((acc, op) => {
              const vendor = op?.vendor_info?.label || '未知厂商';
              const date = op.created_at || service.created_at;
              if (!acc[vendor] || date < acc[vendor]) {
                acc[vendor] = date;
              }
              return acc;
            }, {});

            return {
              ...service,
              operations: operations.sort((a, b) => {
                const labelA = a.vendor_info?.label || '';
                const labelB = b.vendor_info?.label || '';
                return labelA.localeCompare(labelB);
              }),
              vendorEarliestDates,
            };
          })
          .filter(({ tags }) => tags?.length > 0)
          .sort(({ name: c1 }, { name: c2 }) => c1.localeCompare(c2));

        if (isMounted) {
          setServices(processedServices);
          setVendors(extractUniqueVendors(processedServices));
          setTags(extractUniqueCategories(processedServices));
          setShowOperations(true);
          setLoading(false);
        }
      } catch (err) {
        console.error('Failed to fetch operations:', err);
        await fetchServicesData();
      }
    }

    /**
     * 获取服务数据（备用方案）
     */
    async function fetchServicesData() {
      try {
        const response = await fetch(
          `${hostUrl}/api/pay/services?type=serverless&page=1&size=${API_CONFIG.SERVICES_PAGE_SIZE}`,
        );
        const { items } = await response.json();

        const processedServices = items
          .filter(({ tags }) => tags?.length > 0)
          .sort(({ created_at: c1 }, { created_at: c2 }) =>
            c1.localeCompare(c2),
          );

        if (isMounted) {
          setServices(processedServices);
          setTags(extractUniqueCategories(processedServices));
          setLoading(false);
        }
      } catch (err) {
        console.error('Failed to fetch services:', err);
        if (isMounted) {
          setError(err.message);
          setServices([]);
          setLoading(false);
        }
      }
    }

    fetchOperationsData();

    return () => {
      isMounted = false;
    };
  }, [hostUrl]);

  return {
    loading,
    services,
    vendors,
    tags,
    showOperations,
    error,
  };
}

/**
 * 服务筛选 Hook
 */
function useFilteredServices(services, selectedVendor, selectedTag) {
  const [filteredServices, setFilteredServices] = useState([]);
  const [filteredOpsCount, setFilteredOpsCount] = useState(0);

  useEffect(() => {
    // 步骤1：按分类筛选服务
    let servicesByTag = services;
    if (selectedTag !== FILTER_DEFAULTS.ALL) {
      servicesByTag = services.filter((service) => {
        const categoryName = getServiceCategoryName(service.tags || []);
        return categoryName === selectedTag;
      });
    }

    // 步骤2：按厂商筛选操作
    if (selectedVendor === FILTER_DEFAULTS.ALL) {
      // 显示所有厂商的操作
      const processedServices = servicesByTag.map((service) => ({
        ...service,
        filteredOps: mergeOperations(service.operations || []),
      }));

      setFilteredServices(processedServices);
      setFilteredOpsCount(
        processedServices.reduce(
          (total, service) => total + (service.filteredOps?.length || 0),
          0,
        ),
      );
    } else {
      // 只显示选中厂商的操作
      const processedServices = servicesByTag
        .map((service) => ({
          ...service,
          filteredOps: mergeOperations(
            (service.operations || []).filter(
              (op) => op?.vendor_info?.label === selectedVendor,
            ),
          ),
        }))
        .filter((service) => service.filteredOps.length > 0);

      setFilteredServices(processedServices);
      setFilteredOpsCount(
        processedServices.reduce(
          (total, service) => total + service.filteredOps.length,
          0,
        ),
      );
    }
  }, [selectedVendor, selectedTag, services]);

  return {
    filteredServices,
    filteredOpsCount,
  };
}

// ============================================================================
// 主组件
// ============================================================================

/**
 * ServerlessAPIServices 主组件
 * @param {Object} props - 组件属性
 * @param {string} [props.defaultVendor] - 默认厂商筛选值
 * @param {string} [props.defaultTag] - 默认分类筛选值
 */
export default function ServerlessAPIServices({
  defaultVendor,
  defaultTag,
} = {}) {
  const { siteConfig } = useDocusaurusContext();
  const hostUrl = siteConfig.url;

  // 状态管理
  const { selectedVendor, selectedTag, setSelectedVendor, setSelectedTag } =
    useFilterParams(defaultVendor, defaultTag);
  const { loading, services, vendors, tags, showOperations, error } =
    useServicesData(hostUrl);
  const { filteredServices, filteredOpsCount } = useFilteredServices(
    services,
    selectedVendor,
    selectedTag,
  );

  // 加载状态
  if (loading) {
    return <LoadingMessage />;
  }

  // 错误状态
  if (services.length === 0) {
    return <ErrorMessage error={error} />;
  }

  // 正常渲染
  return (
    <>
      <FilterControls
        selectedVendor={selectedVendor}
        selectedTag={selectedTag}
        vendors={vendors}
        tags={tags}
        serviceCount={filteredServices.length}
        operationCount={filteredOpsCount}
        onVendorChange={setSelectedVendor}
        onTagChange={setSelectedTag}
      />
      <ServicesTable
        services={filteredServices}
        showOperations={showOperations}
        hostUrl={hostUrl}
      />
    </>
  );
}
